/*
 * Copyright (c) 2008 Kevin Wetzels
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package be.roam.jindy.ui.pagination;

import java.io.Serializable;

/**
 * Utility class used to iterate through a pagination.
 * 
 * @author Kevin Wetzels
 */
public class Page implements Serializable, Comparable<Page> {

	private static final long serialVersionUID = 7309355062016610352L;

	private int nr;
	
	private Pagination pagination;
	
	private transient Page previous;
	
	private transient Page next;
	
	/**
	 * Constructor.
	 * @param pagination the pagination object
	 * @param nr the number of this page
	 */
	public Page(Pagination pagination, int nr) {
		this.pagination = pagination;
		this.nr = nr;
	}
	
	/**
	 * Constructor.
	 * @param pagination the pagination object
	 * @param nr the number of this page
	 * @param previous the previous page (this constructor will automatically set the next page of this object to <code>this</code>).
	 */
	public Page(Pagination pagination, int nr, Page previous) {
		this.pagination = pagination;
		this.nr = nr;
		this.previous = previous;
		if (this.previous != null) {
			this.previous.next = this;
		}
	}

	/**
	 * @return the number of the page
	 */
	public int getNr() {
		return nr;
	}

	/**
	 * @return the pagination object
	 */
	public Pagination getPagination() {
		return pagination;
	}

	/**
	 * @return <code>true</code> when this is the first page of the pagination
	 */
	public boolean isFirst() {
		return getPrevious() == null;
	}
	
	/**
	 * @return <code>true</code> when this is the current page
	 */
	public boolean isCurrent() {
		return nr == pagination.getCurrentPageNr();
	}
	
	/**
	 * @return <code>true</code> when this is the last page of the pagination
	 */
	public boolean isLast() {
		return getNext() == null;
	}
	
	/**
	 * @return the previous page or <code>null</code> if no such page exists
	 */
	public Page getPrevious() {
		if (previous == null && nr > 1) {
			previous = new Page(pagination, nr - 1, null);
			previous.next = this;
		}
		return previous;
	}	
	
	/**
	 * @return the next page or <code>null</code> if no such page exists
	 */
	public Page getNext() {
		if (next == null && nr < pagination.getNrPages()) {
			next = new Page(pagination, nr + 1, this);
		}
		return next;
	}
	
	@Override
	public int hashCode() {
		return nr;
	}
	
	@Override
	public boolean equals(Object o) {
		if (o instanceof Page) {
			return compareTo((Page) o) == 0;
		}
		return false;
	}

	@Override
	public int compareTo(Page page) {
		if (page == null) {
			return 1;
		}
		return getNr() - page.getNr();
	}

	
}
